<template>
  <div class="el-container">
    <el-container>
      <!-- 头部 -->
      <el-header class="loginOut">
        <h2>电商管理系统</h2>
        <el-button @click="loginOut">退出</el-button>
      </el-header>
      <el-container>
        <!-- 左侧菜单 -->
        <el-aside :width="isChange ? '64px' : '200px'">
          <div class="changeMenu" @click="changeMenu">| | |</div>
          <el-menu
            background-color="#333744"
            text-color="#fff"
            unique-opened
            :collapse="isChange"
            :collapse-transition="false"
            router
            :default-active="activePath"
          >
            <!-- 一级菜单  -->
            <!-- 数值拼接上空字符串 = 字符串 -->
            <el-submenu
              :index="item.id + ''"
              v-for="item in menuList"
              :key="item.id"
            >
              <template slot="title">
                <i :class="iconObj[item.id]"></i>
                <span>{{ item.authName }}</span>
              </template>
              <!-- 二级菜单 -->
              <el-menu-item
                :index="'' + childItem.path"
                v-for="childItem in item.children"
                :key="childItem.id"
                @click="menuItemClick('' + childItem.path)"
              >
                <template slot="title">
                  <i class="el-icon-location"></i>
                  <span>{{ childItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      // 左侧菜单栏数据
      menuList: [],
      // 自定义左侧菜单栏图标
      iconObj: {
        125: 'iconfont icon-user',
        103: 'iconfont icon-3702mima',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-tijikongjian'
      },
      isChange: false,
      activePath: ''
    }
  },
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    menuItemClick(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    changeMenu() {
      this.isChange = !this.isChange
    },
    loginOut() {
      window.sessionStorage.clear()
      this.$message.success('退出成功')
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      // console.log(res)
      // console.log(res.data)
      if (res.meta.status !== 200) {
        this.$$message.error(res.meta.msg)
      } else {
        this.menuList = res.data
      }
    }
  }
}
</script>

<style scoped>
.el-menu {
  border-right: none;
}
.changeMenu {
  height: 30px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  cursor: pointer;
}
.iconfont {
  margin-right: 5px;
}
.el-container {
  height: 100%;
}
.el-header {
  height: 80px;
  background-color: #373741;
}
.el-aside {
  background-color: #333744;
}
.loginOut {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.loginOut h2 {
  color: #fff;
}
</style>
